package com.xunk.equipment.service.dnake.service.impl;

import com.xunk.common.core.constant.DnakeConstants;
import com.xunk.common.core.enums.dnake.DnakeActionEnum;
import com.xunk.common.core.utils.XunkAssert;
import com.xunk.equipment.service.dnake.DnakePublishComponent;
import com.xunk.equipment.service.dnake.service.DnakeSceneService;
import com.xunk.equipment.service.dnake.vo.DnakeLinkageData;
import com.xunk.equipment.service.dnake.vo.DnakeMqttRequestDataMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("dnake-scene")
@Slf4j
public class DnakeSceneServiceImpl implements DnakeSceneService {

    @Autowired
    DnakePublishComponent dnakePublishComponent;

    @Override
    public void ctrlScene(String gatewayDeviceSn, Integer sceneNo) {
        DnakeMqttRequestDataMessage vo = new DnakeMqttRequestDataMessage();
        vo.setSceneNo(sceneNo).setAction(DnakeActionEnum.CTRLSCENE.getCode());
        dnakePublishComponent.publish(vo, gatewayDeviceSn, DnakeConstants.SCENE);
    }

    @Override
    public void setScene(String gatewayDeviceSn, Integer sceneNo, List<DnakeLinkageData> dataList) {
//        DnakeLinkageData dnakeLinkageData = new DnakeLinkageData();
//        dnakeLinkageData.setTimer(1).setAction(DnakeActionEnum.CTRLDEV.getCode())
//                .setColorTemp(0);
//        dataList.add(dnakeLinkageData);

        // 一个场景超过 10 个设备需要分包发送
        List<List<DnakeLinkageData>> result  = splitList(dataList,10);
        final int[] i = {0};
        result.forEach(data -> {
            DnakeMqttRequestDataMessage vo = new DnakeMqttRequestDataMessage();
            vo.setSceneNo(sceneNo)
//                .setUserId("")// 不用传
                    .setSn(i[0])
                    .setEnable(1)
                    .setLinkage(data)
                    .setAction(DnakeActionEnum.SETSCENE.getCode());
            dnakePublishComponent.publish(vo, gatewayDeviceSn, DnakeConstants.SCENE);
            i[0]++;
        });

    }

    @Override
    public void delScene(String gatewayDeviceSn, Integer sceneNo) {
        XunkAssert.notEmpty(sceneNo, "场景号必须传！");
        XunkAssert.notEmpty(gatewayDeviceSn, "网关SN号必须传！");

        DnakeMqttRequestDataMessage vo = new DnakeMqttRequestDataMessage();
        vo.setSceneNo(sceneNo).setAction(DnakeActionEnum.DELSCENE.getCode());
        dnakePublishComponent.publish(vo, gatewayDeviceSn, DnakeConstants.SCENE);
    }

    @Override
    public void delAllScene(String gatewayDeviceSn) {
        dnakePublishComponent
                .simplePublish(DnakeActionEnum.DELALLSCENE.getCode(), gatewayDeviceSn, DnakeConstants.SCENE);
    }


    /**
     * 非均分，最后一组数量可能小于其它组数量
     * @param list
     * @param unitSize
     * @return
     */
    private <T> List<List<T>> splitList(List<T> list, int unitSize) {
        if (list == null || list.isEmpty() || unitSize < 1)
            return new ArrayList<>();

        int wholeSize = list.size();
        List<List<T>> result = new ArrayList<>();

        int start = 0;
        int end;
        while(start < wholeSize){
            end = start + unitSize;
            if(end > wholeSize){
                end = wholeSize;
            }
            List<T> subList = list.subList(start, end);
            result.add(subList);
            start = start + unitSize;
        }
        return result;
    }
}
